const state = () => ({
  tabs: [],
});

const getters = {};

const actions = {};

const mutations = {
  setTab: (state, tab) => {
    if (state.tabs.some((item) => item.path === tab.path)) {
      return;
    }
    state.tabs.push(tab);
  },
  removeCurrentTab: (state, tab) => {
    state.tabs = state.tabs.filter((item) => item.path !== tab.path);
  },
  removeNextTab: (state, tab) => {
    const tabIndex = state.tabs.findIndex((item) => item.path === tab.path);
    if (tabIndex != state.tabs.length) {
      state.tabs = state.tabs.filter((item, index) => index != tabIndex + 1);
    }
  },
  removeAllTab: (state, tab) => {
    state.tabs = [];
    state.tabs.push(tab);
  },
  removePrevTab: (state, tab) => {
    const tabIndex = state.tabs.findIndex((item) => item.path === tab.path);
    if (tabIndex != 0) {
      state.tabs = state.tabs.filter((item, index) => index != tabIndex - 1);
    }
  },
};

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations,
};
